package com.library.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.dto.DashboardVO;
import com.library.entity.Book;
import com.library.entity.BorrowRecord;
import com.library.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仪表盘服务
 */
@Service
@RequiredArgsConstructor
public class DashboardService {
    
    private final BookService bookService;
    private final BorrowRecordService borrowRecordService;
    private final UserService userService;
    
    /**
     * 获取仪表盘数据
     */
    public DashboardVO getDashboardData() {
        DashboardVO vo = new DashboardVO();
        
        // 总图书量
        vo.setTotalBooks(bookService.count());
        
        // 已借出量
        Long borrowedBooks = borrowRecordService.count(new LambdaQueryWrapper<BorrowRecord>()
                .eq(BorrowRecord::getStatus, "BORROWED"));
        vo.setBorrowedBooks(borrowedBooks);
        
        // 逾期数量
        Long overdueBooks = borrowRecordService.count(new LambdaQueryWrapper<BorrowRecord>()
                .eq(BorrowRecord::getStatus, "OVERDUE"));
        vo.setOverdueBooks(overdueBooks);
        
        // 总用户数
        vo.setTotalUsers(userService.count());
        
        // 热门图书排行榜
        List<Book> hotBooks = bookService.getHotBooks(10);
        vo.setHotBooks(hotBooks);
        
        // 最近借阅记录
        Page<BorrowRecord> recentRecords = borrowRecordService.pageRecords(1, 10, null, null);
        vo.setRecentRecords(recentRecords.getRecords());
        
        return vo;
    }
}

